package org.qloudgen.borg.pages.compnent;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.wicket.validation.IErrorMessageSource;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidationError;
import org.apache.wicket.validation.IValidator;

public class ParamDateValidator implements IValidator<String> {

	private static final long serialVersionUID = 1L;
	private SimpleDateFormat format;
	private String dateFormat;
	
	public ParamDateValidator(String dateFormat){
		this.dateFormat = dateFormat;
		this.format = new SimpleDateFormat(dateFormat);
	}
	
	@Override
	public void validate(IValidatable<String> validatable) {
		
		boolean flag = true; 
		
		String value = validatable.getValue().toString();
System.out.println(value);		
		try {
			format.parse(value);
			flag = false;
System.out.println("date error");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (flag) {
			
			IValidationError error = new IValidationError(){

				private static final long serialVersionUID = 1L;

				@Override
				public Serializable getErrorMessage(
						IErrorMessageSource messageSource) {
System.out.println("date error");
					return "非法日期格式，必须是"+ dateFormat;
				}
				
			};
			
			validatable.error(error);
		}
	}

}
